﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TraitorAmongUs.Graphics {

	public class Sprite {
		// Sprite parameter definitions
		protected Texture2D texture;

		protected int width;
		public int Width {
			get {
				return width;
			}

			set {
				width = value;
			}
		}

		protected int height;
		public int Height {
			get {
				return height;
			}

			set {
				height = value;
			}
		}

		public int TextureWidth {
			get {
				return (texture != null ? texture.Width : 0);
			}
		}

		public int TextureHeight {
			get {
				return (texture != null ? texture.Height : 0);
			}
		}

		protected int x;
		public int X {
			get {
				return x;
			}

			set {
				x = value;
			}
		}

		protected int y;
		public int Y {
			get {
				return y;
			}

			set {
				y = value;
			}
		}



		protected int numberOfFrames;
		protected int currentFrame;
		protected double timer;

		protected double timePerFrame;

		/// <summary>
		/// Creates an empty sprite object, for use on the serverside
		/// </summary>
		public Sprite() {
		}

		/// <summary>
		/// Creates a sprite object from an existing texture
		/// </summary>
		/// <param name="tex">The texture of the sprite</param>
		public Sprite(Texture2D tex) {
			texture = tex;
			width = texture.Width;
			height = texture.Height;

			currentFrame = 0;

			timePerFrame = 200.0;
			timer = 0.0;
		}

		/// <summary>
		/// Creates a sprite object from the content pipeline
		/// </summary>
		/// <param name="content">The content manager</param>
		/// <param name="assetName">The name of the asset</param>
		public Sprite(ContentManager content, string assetName) {
			texture = content.Load<Texture2D>(assetName);
			width = texture.Width;
			height = texture.Height;

			currentFrame = 0;

			timePerFrame = 200.0;
			timer = 0.0;
		}

		/// <summary>
		/// Updates the sprite's animation
		/// </summary>
		/// <param name="gameTime">The current elapsed time</param>
		public virtual void Update(GameTime gameTime) {
			if(texture == null) return;

			timer += gameTime.ElapsedGameTime.TotalMilliseconds;

			if(timer >= timePerFrame) {
				// Advance the frame
				timer = 0.0;

				numberOfFrames = (int)(((float)texture.Width) / width);
				currentFrame = (currentFrame < numberOfFrames - 1 ? currentFrame + 1 : 0);
			}
		}

		/// <summary>
		/// Renders the sprite
		/// </summary>
		/// <param name="gameTime">The current elapsed time</param>
		public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch, Color? color = null) {
			if(texture == null) return;

			var spriteRect = new Rectangle(
				currentFrame * width,
				0,
				width,
				height
			);

			spriteBatch.Draw(texture, new Rectangle(x, y, width, height), spriteRect, (color.HasValue ? color.Value : Color.White));
		}
	}
}
